package com.ssc.nav.task

import com.ssc.nav.extension.NavPluginExtension
import com.ssc.nav.utils.JavaCreateUtils
import com.ssc.nav.utils.LogUtils
import com.ssc.nav.utils.XmlCreateUtils
import com.ssc.nav.utils.XmlPullParserUtils
import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

/**
 * user: jyt
 * DATA: 2024/9/5
 * TIME: 10:13
 */

abstract class NavTask : DefaultTask() {

    @get:Input
    abstract val config: Property<NavPluginExtension>

    @TaskAction
    fun run() {
        val target = project
        val extension =config.get()
        /**
         * 读取目标文件
         * /src/main/res/values/ids.xml
         */
        val readDir = extension.xmlIdsFile
        if (readDir.exists()) {
            val readText = readDir.readText()

            LogUtils.d(extension.isDebugLog,"读取文件内容：$readText")
            /**
             * 读取xml中的id
             */
            val map = XmlPullParserUtils.xmlRead(extension,readDir)
            /**
             * 生成navigation导航xml
             */
            XmlCreateUtils.createNavXml(extension, map)
            /**
             * 生成对应nav id的变量类
             */
            JavaCreateUtils.createNavIdClass(extension,target, map)

        } else {
            LogUtils.d(extension.isDebugLog,"读取不到文件内容" + readDir.absolutePath)
        }
    }


}